package com.lyl.controller;

import com.lyl.domain.SysMenu;
import com.lyl.model.Result;
import com.lyl.model.RolePrivilegeParam;
import com.lyl.service.SysRolePrivilegeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import netscape.security.Privilege;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author lylbell
 * @date 2021-06-23 15:38
 */
@Api(tags = "角色权限管理")
@RestController
public class SysRolePrivilegeController {

    @Autowired
    private SysRolePrivilegeService sysRolePrivilegeService;

    /**
     * 通过角色获取菜单权限
     * @param roleId
     * @return
     */
    @GetMapping("/roles_privileges")
    @ApiOperation(value="通过角色获取菜单权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId",value = "角色id")
    })
    public Result<List<SysMenu>> findMenuAndPrivilegeByRoleId(Long roleId) {
        List<SysMenu> menus = sysRolePrivilegeService.findMenuAndPrivilegeByRoleId(roleId);
        return Result.ok(menus);
    }


    @PostMapping("/grant_privileges")
    @ApiOperation(value="给角色授予某种权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rolePrivilegeParam",value = "角色权限参数的json数据")
    })
    public Result grantPrivilege(@RequestBody RolePrivilegeParam rolePrivilegeParam) {
       Boolean isOk = sysRolePrivilegeService.grantPrivilege(rolePrivilegeParam);
       if (isOk) {
           return Result.ok("授权成功");
       }
       return Result.fail("授权失败");
    }
}
